"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具读取磁阵文件内容之-o offset参数验证
Create At   : 2023/4/13
Owner       : opentestcase019
Description :
    1.读取磁阵文件内容，指定-o offset参数为有效值
    2.读取磁阵文件内容，指定-o offset参数为无效值
Expect      :
    1.读取成功
    2.合理报错
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()

    def test_sharestorage_tool(self):
        step1 = "-----step1:读取磁阵文件内容，指定-o offset参数为有效值;expect:读取成功-----"
        self.logger.info(step1)
        self.logger.info("-----获取磁阵文件-----")
        shell_cmd = f"source {macro.DB_ENV_PATH};" \
                    f"cat {macro.DSS_INSTANCE}/cfg/dss_vg_conf.ini"
        self.logger.info(shell_cmd)
        shell_msg = self.common.get_sh_result(self.primary_node, shell_cmd)
        file1 = shell_msg.splitlines()[0].split(':')[-1].strip()
        file2 = shell_msg.splitlines()[1].split(':')[-1].strip()
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd dev -p {file1} -o 512 -f c;" \
                      f"dsscmd dev -p {file2} -o 0 -f h;" \
                      f"dsscmd dev -p {file2} -o '512' -f h;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"filename is {file1}, offset is 512.",
                      execute_msg, "执行失败" + step1)
        self.assertIn(f"filename is {file2}, offset is 0.",
                      execute_msg, "执行失败" + step1)
        self.assertIn(f"filename is {file2}, offset is 512.",
                      execute_msg, "执行失败" + step1)

        step2 = "-----step2:读取磁阵文件内容，指定-o offset参数为无效值;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = ["-o '-1' -f c", "-o 512.245 -f u", "-o 125 -f s",
                    "-o #56 -f s", "-o 512* -f s", "-o $* -f s", '-o zero -f s'
                    ]
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                          f"dsscmd dev -p {file1} {cmd};"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == cmd_list[3]:
                self.assertIn("-o should with args.",
                              execute_msg, "执行失败" + step2)
            elif cmd == cmd_list[0] or cmd == cmd_list[2]:
                self.assertIn("offset must be >= 0 and be align 512.",
                              execute_msg, "执行失败" + step2)
            else:
                self.assertIn("The value of offset is invalid.",
                              execute_msg, "执行失败" + step2)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
